package com.ggh.jinjilive.ui.mall;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentMallBinding;
import com.ggh.jinjilive.listener.AddOnClickListener1;
import com.ggh.jinjilive.mobile.CommonMobile;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.mall.community.CommunityFragment;
import com.ggh.jinjilive.ui.mall.goods.GoodsFragment;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.ggh.jinjilive.utils.GeneralUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class MallFragment extends BaseFragment<FragmentMallBinding> implements View.OnClickListener {
    //
    private String[] mFragmentTitles = {"商品", "社区"};
    //
    private List<Fragment> fragmentList = new ArrayList<>();
    //商品
    private GoodsFragment goodsFragment;
    //社区
    private CommunityFragment communityFragment;

    private ProfitOrderAdapter profitOrderAdapter;
    //店铺状态
    private CommonMobile commonMobile;
    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_mall;
    }

    @Override
    public void initView() {
        super.initView();
        //获取状态栏高度
        binding.llSpacing.setPadding(0, BarUtils.getStatusBarHeight(),0,0);
        //清理
        fragmentList.clear();
        //商品
        goodsFragment = new GoodsFragment();
        fragmentList.add(goodsFragment);
        //社区
        communityFragment = new CommunityFragment();
        fragmentList.add(communityFragment);

        profitOrderAdapter = new ProfitOrderAdapter(getChildFragmentManager());
        binding.vp.setAdapter(profitOrderAdapter);
        binding.stlOrder.setViewPager(binding.vp, mFragmentTitles);
        binding.vp.setCurrentItem(0);
        //获取店铺状态
        GeneralUtils.checkStoreStatus(new AddOnClickListener1() {
            @Override
            public void onSkipClicked(Object mObject) {
                commonMobile = (CommonMobile) mObject;
                if(null != commonMobile){
                    ////0未入住  1申请中  2已入住
                    if(commonMobile.getType() == 0){
                        binding.tvStore.setText("创建店铺");
                    }else if(commonMobile.getType() == 1){
                        binding.tvStore.setText("审核中");
                    }else {
                        binding.tvStore.setText("我的店铺");
                    }
                }
            }
        });
    }


    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //我的店铺
        binding.llStore.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll_store://店铺
                if(commonMobile.getType() == 0){
                    ToastUtils.make().show("创建店铺");
                }else if(commonMobile.getType() == 1){
                    ToastUtils.make().show("审核中");
                }else {
                    ARouter.getInstance().build(ARouterUtils.MineShopActivity)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            .navigation();
                }
                break;
        }
    }


    class ProfitOrderAdapter extends FragmentPagerAdapter {

        public ProfitOrderAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public void destroyItem(@NonNull @NotNull ViewGroup container, int position, @NonNull @NotNull Object object) {
//            super.destroyItem(container, position, object);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles[position];
        }

        @Override
        public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            super.setPrimaryItem(container, position, object);
        }
    }

}
